package com.example.service.leave;

import com.example.data.leave.Leave;
import com.example.data.leave.LeaveBO;
import com.example.repository.LeaveRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class LeaveServiceImpl implements LeaveService{
    private LeaveRepository leaveRepository;
    @Autowired
    public LeaveServiceImpl(LeaveRepository leaveRepository){
        this.leaveRepository = leaveRepository;
    }
    @Override
    public List<Leave> getAllLeaveInfo(){
        Leave leaveInfo = LeaveBO.convert(leaveRepository.getAllLeaveInfo().get(0));
        return Arrays.asList(leaveInfo);
    }
    @Override
    public List<Leave> getLeaveInfoByEmpID(long empID){
        Leave leaveInfo = LeaveBO.convert(leaveRepository.getLeaveInfoByEmpID(empID));
        return Arrays.asList(leaveInfo);
    }
    @Override
    public List<Leave> addLeaveInfo(Leave leaveVO){
        Leave leave = LeaveBO.convert(leaveVO);
        Leave leaveInfo = LeaveBO.convert(leaveRepository.addLeaveInfo(leave));
        return Arrays.asList(leaveInfo);
    }
    @Override
    public List<Leave> editLeaveInfo(Leave leaveVO){
        long empID = leaveVO.getEmpID();
        long appID = leaveVO.getAppID();
        Map<String, Object> leaveMap = leaveRepository.QueryLeaveInfoByID(empID, appID);
        Leave leaveBO = new LeaveBO();
        BOFilled(leaveBO, leaveMap);
        BOUpdate(leaveBO, leaveVO);
        Leave leaveInfo = LeaveBO.convert(leaveRepository.editLeaveInfo(leaveBO).get(0));
        return Arrays.asList(leaveInfo);
    }
    @Override
    public void deleteLeaveInfo(long appID, long empID){
        leaveRepository.deleteLeaveInfo(appID, empID);
    }

    //获取已有请假信息
    public void BOFilled(Leave leaveBO, Map<String, Object> leaveInfo){
        for(Map.Entry<String, Object> entry: leaveInfo.entrySet()){
            switch (entry.getKey()) {
                case "empID":
                    leaveBO.setEmpID((Long)entry.getValue());
                    break;
                case "appID":
                    leaveBO.setAppID((Long)entry.getValue());
                    break;
                case "empName" :
                    leaveBO.setEmpName((String)entry.getValue());
                    break;
                case "empDeptID" :
                    leaveBO.setEmpDeptID((Long)entry.getValue());
                    break;
                case "empDeptName" :
                    leaveBO.setEmpDeptName((String)entry.getValue());
                    break;
                case "reason" :
                    leaveBO.setReason((String) entry.getValue());
                    break;
                case "type" :
                    leaveBO.setType((Integer)entry.getValue());
                    break;
                case "state" :
                    leaveBO.setState((Integer)entry.getValue());
                    break;
            }
        }

    }
    //用从前端接受的VO更新请假信息
    public void BOUpdate(Leave leaveBO, Leave leaveVO){

        if(leaveVO.getEmpName() != null){
            leaveBO.setEmpName(leaveVO.getEmpName());
        }
        if(leaveVO.getEmpDeptID() != null){
            leaveBO.setEmpDeptID(leaveVO.getEmpDeptID());
        }
        if(leaveVO.getEmpDeptName() != null){
            leaveBO.setEmpDeptName(leaveVO.getEmpDeptName());
        }
        if(leaveVO.getReason() != null){
            leaveBO.setReason(leaveVO.getReason());
        }
        if(leaveVO.getType() != null){
            leaveBO.setType(leaveVO.getType());
        }
        if(leaveVO.getState() != null){
            leaveBO.setState(leaveVO.getState());
        }
    }
}
